package com.hxk.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * spring服务端给客户端相应数据的方法
 * @author Cactus
 */
@Controller
public class ResponseParamController {

	/**
	 * 原始方式
	 * @return
	 */
	@RequestMapping("respp1")
	public String respp1(HttpServletRequest req, HttpSession session){
		req.setAttribute("req", "req值");
		session.setAttribute("ses", "session的值");
		ServletContext application = req.getServletContext();
		application.setAttribute("app", "application值");
		return "home";
	}
	/**
	 * 使用 SpringMVC 中 Model 接口
	 * 3.1 把内容最终放入到 request 作用域中.
	 * @param model
	 * @return
	 */
	@RequestMapping("respp2")
	public String respp2(Model model){
		model.addAttribute("model", "model的值");
		return "home";
	}
	
	/**
	 * 使用 Map 集合
		2.1 把 map 中内容放在 request 作用域中
		2.2 spring 会对 map 集合通过 BindingAwareModelMap 进行实例化
	 * @param map
	 * @return
	 */
	@RequestMapping("respp3")
	public String respp3(Map<String, Object> map){
		map.put("map", "map的值");
		return "home";
	}
	/**
	 * 使用 SpringMVC 中 ModelAndView 类
	 * @return
	 */
	@RequestMapping("respp4")
	public ModelAndView respp4(){
		ModelAndView modelAndView = new ModelAndView("home");
		modelAndView.addObject("mav", "modelAndView的值");
		return modelAndView;
	}
	/**
	 * 不跳转页面，只传递数据，并且以json字符串格式返回数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("respp5")
	public Map<String, Object> respp5(){
		Map<String, Object> rs = new HashMap<>();
		rs.put("age", 15);
		rs.put("nam", "lisi");
		return rs;
	}
}
